//
//  ScanView.m
//  PersonalManager
//
//  Created by user on 16/8/16.
//  Copyright © 2016年 Haiyun.Qian. All rights reserved.
//

#import "ScanView.h"

#define ScanWidth (self.bounds.size.width - 175)

@implementation ScanView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.blankArea = CGRectMake(self.center.x - ScanWidth/2 , self.center.y - ScanWidth/2 - 94, ScanWidth, ScanWidth);
    }
    return self;
}

- (void)layoutSubviews {
    if (!self.scanImageView) {
        self.scanImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.blankArea.origin.x, self.blankArea.origin.y, self.blankArea.size.width, 30)];
        self.scanImageView.image = [UIImage imageNamed:@"qr_scan_line"];
        self.scanImageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:self.scanImageView];
    }
    if (!self.timer) {
        //设置定时器
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0/50 target:self selector:@selector(changeScanImageViewY) userInfo:nil repeats:YES];
        [self.timer fire];
    }
}
//改变扫描白条让其上下移动
- (void)changeScanImageViewY {
    CGRect rect = self.scanImageView.frame;
    if ((++ rect.origin.y) >= (self.blankArea.origin.y + self.blankArea.size.height - 15)) {
        rect.origin.y = self.blankArea.origin.y - 15;
    }
    self.scanImageView.frame = rect;
}


- (void)drawRect:(CGRect)rect {
    CGContextRef ref = UIGraphicsGetCurrentContext();
    [self addScreenFillRectWithRef:ref Rect:self.frame];
    [self addCenterClearRectWithRef:ref Rect:self.blankArea];
    [self addWhiteRectWithRef:ref Rect:self.blankArea];
    [self addCornerLineWithRef:ref Rect:self.blankArea];
}

//全屏暗黑
- (void)addScreenFillRectWithRef:(CGContextRef)ref Rect:(CGRect)rect{
    CGContextSetRGBFillColor(ref, 40 / 255.0, 40 / 255.0, 40 / 255.0, 0.5);
    CGContextFillRect(ref, rect);
}

//清空中间二维码区域
- (void)addCenterClearRectWithRef:(CGContextRef)ref Rect:(CGRect)rect {
    CGContextClearRect(ref, rect);
}

//中间二维码边框
- (void)addWhiteRectWithRef:(CGContextRef)ref Rect:(CGRect)rect {
    CGContextStrokeRect(ref, rect);
    CGContextSetRGBStrokeColor(ref, 1, 1, 1, 1);
    CGContextSetLineWidth(ref, 0.8);
    CGContextAddRect(ref, rect);
    CGContextStrokePath(ref);
}

//画边框的四个角为绿色
- (void)addCornerLineWithRef:(CGContextRef)ref Rect:(CGRect)rect{
    //设置线粗细,颜色绿色
    CGContextSetLineWidth(ref, 5);
    CGContextSetRGBStrokeColor(ref, 83 / 255.0, 239 / 255.0, 111 / 255.0, 1);
    
    //左上角
    CGPoint poinsTopLeftA[] = {
        CGPointMake(rect.origin.x+0.7, rect.origin.y),
        CGPointMake(rect.origin.x+0.7 , rect.origin.y + 15)
    };
    CGPoint poinsTopLeftB[] = {CGPointMake(rect.origin.x, rect.origin.y +0.7),CGPointMake(rect.origin.x + 15, rect.origin.y+0.7)};
    [self addLine:poinsTopLeftA pointB:poinsTopLeftB ref:ref];
    
    //左下角
    CGPoint poinsBottomLeftA[] = {CGPointMake(rect.origin.x+ 0.7, rect.origin.y + rect.size.height - 15),CGPointMake(rect.origin.x +0.7,rect.origin.y + rect.size.height)};
    CGPoint poinsBottomLeftB[] = {CGPointMake(rect.origin.x , rect.origin.y + rect.size.height - 0.7) ,CGPointMake(rect.origin.x+0.7 +15, rect.origin.y + rect.size.height - 0.7)};
    [self addLine:poinsBottomLeftA pointB:poinsBottomLeftB ref:ref];
    
    //右上角
    CGPoint poinsTopRightA[] = {CGPointMake(rect.origin.x+ rect.size.width - 15, rect.origin.y+0.7),CGPointMake(rect.origin.x + rect.size.width,rect.origin.y +0.7 )};
    CGPoint poinsTopRightB[] = {CGPointMake(rect.origin.x+ rect.size.width-0.7, rect.origin.y),CGPointMake(rect.origin.x + rect.size.width-0.7,rect.origin.y + 15 +0.7 )};
    [self addLine:poinsTopRightA pointB:poinsTopRightB ref:ref];
    //右下角
    CGPoint poinsBottomRightA[] = {CGPointMake(rect.origin.x+ rect.size.width -0.7 , rect.origin.y+rect.size.height+ -15),CGPointMake(rect.origin.x-0.7 + rect.size.width,rect.origin.y +rect.size.height )};
    CGPoint poinsBottomRightB[] = {CGPointMake(rect.origin.x+ rect.size.width - 15 , rect.origin.y + rect.size.height-0.7),CGPointMake(rect.origin.x + rect.size.width,rect.origin.y + rect.size.height - 0.7 )};
    [self addLine:poinsBottomRightA pointB:poinsBottomRightB ref:ref];
    CGContextStrokePath(ref);
}

- (void) addLine:(CGPoint[])pointA pointB:(CGPoint[])pointB ref:(CGContextRef)ref {
    CGContextAddLines(ref, pointA, 2);
    CGContextAddLines(ref, pointB, 2);
}

@end
